from crypt import methods

from django.db.models import F
from django.shortcuts import render
from rest_framework import status
from rest_framework.decorators import action
from rest_framework.mixins import ListModelMixin
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.viewsets import GenericViewSet, ModelViewSet

from community.models import Label, Article, Comment
from community.paginations import ArticleLimitOffsetPagination, ArticlePageNumberPagination
from community.serializers import LableSerializer, ArticleSerializer, CommentSerializer, SonCommentSerializer


# Create your views here.


# 标签视图集
# 标签不需要增删改
class LabelViewSet(GenericViewSet,ListModelMixin):
    # 查询集
    queryset = Label.objects.all()
    # 序列化器
    serializer_class = LableSerializer
    # 登录权限
    permission_classes = [IsAuthenticated]





# 文章视图集
class ArticleViewSet(ModelViewSet):
    # 过滤未发布文章
    queryset = Article.objects.filter(is_delete=False)
    serializer_class = ArticleSerializer
    # 登录权限
    permission_classes = [IsAuthenticated]
    # 分页器权限
    pagination_class = ArticleLimitOffsetPagination

    # 未发布文章进行隐藏
    def get_queryset(self):
        # list 和 retrieve 都是查看方法
        if self.action in ["list", "retrieve"]:
            return self.queryset.filter(status = 1)
        else:
            return self.queryset


    def get_serializer_class(self):
        if self.action in ["comment"]:
            return CommentSerializer
        else:
            return self.serializer_class

    # 重写create方法 评论用户为当前登录用户
    def create(self, request, *args, **kwargs):
        request.POST._mutable = True
        request.data["user"] = request.user.id
        # 设置完 重调 create方法创建文章
        return ModelViewSet.create(self, request, *args, **kwargs)


    # 重写retrive 方法
    def retrieve(self, request, *args, **kwargs):
        self.get_queryset().filter(id = kwargs["pk"]).update(page_view=F("page_view")+1)
        return ModelViewSet.retrieve(self, request, *args, **kwargs)




    # 获取一级评论
    @action(methods=["get"],detail=True)
    def comment(self,request,pk):
        try:
            article = self.get_queryset().get(pk=pk)
        except Article.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)
        # 通过文章管理器获取一级评论
        comments = article.comment_set.filter(parent_comment__isnull=True)
        comments_page = self.paginate_queryset(comments)
        serializer = self.get_serializer(comments_page, many=True)
        return self.get_paginated_response(serializer.data)
# 评论视图集

class CommentViewSet(ModelViewSet):
    queryset = Comment.objects.filter(article__status=1,article__is_delete=False,is_delete=False)
    serializer_class = CommentSerializer
    # 登录权限
    permission_classes = [IsAuthenticated]
    pagination_class = ArticleLimitOffsetPagination

    # 重定向序列化器
    def get_serializer_class(self):
        if self.action in ["retrieve"]:
            return SonCommentSerializer
        else:
            return self.serializer_class

    # 重写create方法 发布文章用户为当前登录用户
    # def create(self, request, *args, **kwargs):
    #     request.POST._mutable = True
    #     request.data["user"] = request.user.id
    #     # 设置完 重调 create方法创建文章
    #     return ModelViewSet.create(self, request, *args, **kwargs)

    # 查看评论详情
    def retrieve(self, request, *args, **kwargs):
        try:
            comment = self.get_queryset().get(pk=kwargs["pk"])
        except Comment.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)
        # 通过模型管理器获取子级评论
        comments = comment.parent_comment_set.all()
        comments_page = self.paginate_queryset(comments)
        # 序列化器 序列化
        serializer = self.get_serializer(comments_page, many=True)
        return self.get_paginated_response(serializer.data)